package com.coin.exchange.okex.rest.stock.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.coin.exchange.ok.vo.OkAccountRecordsRequestVO;
import com.coin.exchange.ok.vo.OkCancelWithdrawRequestVO;
import com.coin.exchange.ok.vo.OkFundsTransferRequestVO;
import com.coin.exchange.ok.vo.OkKlineRequestVO;
import com.coin.exchange.ok.vo.OkWalletInfoRequestVO;
import com.coin.exchange.ok.vo.OkWithdrawInfoRequestVO;
import com.coin.exchange.ok.vo.OkWithdrawRequestVO;
import com.coin.exchange.okcoin.rest.HttpUtil;
import com.coin.exchange.okcoin.rest.HttpUtilManager;
import com.coin.exchange.okcoin.rest.MD5Util;
import com.coin.exchange.okcoin.rest.StringUtil;
import com.coin.exchange.okex.rest.stock.IStockRestApi;

@Service
public class OkexStockRestApi implements IStockRestApi {

	@Value("${okex.api.url}")
	private String url_prex;
	
	/**
	 * 现货行情URL
	 */
	private final String TICKER_URL = "/api/v1/ticker.do";
	
	/**
	 * 现货市场深度URL
	 */
	private final String DEPTH_URL = "/api/v1/depth.do";
	
	/**
	 * 现货历史交易信息URL
	 */
	private final String TRADES_URL = "/api/v1/trades.do";
	
	private final String KLINE_URL = "/api/v1/kline.do";
	
	/**
	 * 现货获取用户信息URL
	 */
	private final String USERINFO_URL = "/api/v1/userinfo.do?";
	
	/**
	 * 现货 下单交易URL
	 */
	private final String TRADE_URL = "/api/v1/trade.do?";
	
	/**
	 * 现货 批量下单URL
	 */
	private final String BATCH_TRADE_URL = "/api/v1/batch_trade.do";
	
	/**
	 * 现货 撤销订单URL
	 */
	private final String CANCEL_ORDER_URL = "/api/v1/cancel_order.do";
	
	/**
	 * 现货 获取用户订单URL
	 */
	private final String ORDER_INFO_URL = "/api/v1/order_info.do";
	
	/**
	 * 现货 批量获取用户订单URL
	 */
	private final String ORDERS_INFO_URL = "/api/v1/orders_info.do";
	
	/**
	 * 现货 获取历史订单信息，只返回最近七天的信息URL
	 */
	private final String ORDER_HISTORY_URL = "/api/v1/order_history.do";
	
	private final String WITHDRAW_URL = "/api/v1/withdraw.do";
	private final String CANCEL_WITHDRAW_URL = "/api/v1/cancel_withdraw.do";
	private final String WITHDRAW_INFO_URL = "/api/v1/withdraw_info.do";
	private final String ACCOUNT_RECORDS_URL = "/api/v1/account_records.do";
	private final String FUNDS_TRANSFER_URL = "/api/v1/funds_transfer.do";
	private final String WALLET_INFO_URL = "/api/v1/wallet_info.do";

	@Override
	public String ticker(String symbol) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("symbol", symbol);
		String result = HttpUtil.get(url_prex + TICKER_URL, params);
	    return result;
	}

	@Override
	public String depth(String symbol) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("symbol", symbol);
		String result = HttpUtil.get(url_prex + DEPTH_URL, params);
	    return result;
	}

	@Override
	public String trades(String symbol, String since) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("symbol", symbol);
		if (!StringUtil.isEmpty(since )) {
			params.put("since", since);
		}
		String result = HttpUtil.get(url_prex + TRADES_URL, params);
	    return result;
	}

	@Override
	public String kline(OkKlineRequestVO vo) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("symbol", vo.getSymbol());
		params.put("type", vo.getType());
		if (vo.getSize() != null) {
			params.put("size", vo.getSize());
		}
		if (vo.getSince() != null) {
			params.put("since", vo.getSince());
		}
		String result = HttpUtil.get(url_prex + KLINE_URL, params);
	    return result;
	}

	@Override
	public String userinfo(String api_key, String secret_key) throws HttpException, IOException {
		// 构造参数签名
		Map<String, String> params = new HashMap<String, String>();
		params.put("api_key", api_key);
		String sign = MD5Util.buildMysignV1(params, secret_key);
		params.put("sign", sign);
		
		// 发送post请求
		HttpUtilManager httpUtil = HttpUtilManager.getInstance();
		String result = httpUtil.requestHttpPost(url_prex, this.USERINFO_URL, params);

		return result;
	}

	@Override
	public String trade(String api_key, String secret_key, String symbol, String type, String price, String amount) throws HttpException, IOException {
		// 构造参数签名
		Map<String, String> params = new HashMap<String, String>();
		params.put("api_key", api_key);
		if(!StringUtil.isEmpty(symbol)){
			params.put("symbol", symbol);
		}
		if(!StringUtil.isEmpty(type)){
			params.put("type", type);
		}
		if(!StringUtil.isEmpty(price)){
			params.put("price", price);
		}
		if(!StringUtil.isEmpty(amount)){
			params.put("amount", amount);
		}
		String sign = MD5Util.buildMysignV1(params, secret_key);
		params.put("sign", sign);
		
		// 发送post请求
		HttpUtilManager httpUtil = HttpUtilManager.getInstance();
		String result = httpUtil.requestHttpPost(url_prex, this.TRADE_URL, params);

		return result;
	}

	@Override
	public String batch_trade(String api_key, String secret_key, String symbol, String type, String orders_data) throws HttpException, IOException {
		// 构造参数签名
		Map<String, String> params = new HashMap<String, String>();
		params.put("api_key", api_key);
		if(!StringUtil.isEmpty(symbol)){
			params.put("symbol", symbol);
		}
		if(!StringUtil.isEmpty(type)){
			params.put("type", type);
		}
		if(!StringUtil.isEmpty(orders_data)){
			params.put("orders_data", orders_data);
		}
		String sign = MD5Util.buildMysignV1(params, secret_key);
		params.put("sign", sign);
		
		// 发送post请求
		HttpUtilManager httpUtil = HttpUtilManager.getInstance();
		String result = httpUtil.requestHttpPost(url_prex, this.BATCH_TRADE_URL, params);

		return result;
	}

	@Override
	public String cancel_order(String api_key, String secret_key, String symbol, String order_id) throws HttpException, IOException {
		// 构造参数签名
		Map<String, String> params = new HashMap<String, String>();
		params.put("api_key", api_key);
		if(!StringUtil.isEmpty(symbol)){
			params.put("symbol", symbol);
		}
		if(!StringUtil.isEmpty(order_id)){
			params.put("order_id", order_id);
		}

		String sign = MD5Util.buildMysignV1(params, secret_key);
		params.put("sign", sign);
		
		// 发送post请求
		HttpUtilManager httpUtil = HttpUtilManager.getInstance();
		String result = httpUtil.requestHttpPost(url_prex, this.CANCEL_ORDER_URL, params);

		return result;
	}

	@Override
	public String order_info(String api_key, String secret_key, String symbol, String order_id) throws HttpException, IOException {
		// 构造参数签名
		Map<String, String> params = new HashMap<String, String>();
		params.put("api_key", api_key);
		if(!StringUtil.isEmpty(symbol)){
			params.put("symbol", symbol);
		}
		if(!StringUtil.isEmpty(order_id)){
			params.put("order_id", order_id);
		}

		String sign = MD5Util.buildMysignV1(params, secret_key);
		params.put("sign", sign);
		
		// 发送post请求
		HttpUtilManager httpUtil = HttpUtilManager.getInstance();
		String result = httpUtil.requestHttpPost(url_prex, this.ORDER_INFO_URL, params);

		return result;
	}

	@Override
	public String orders_info(String api_key, String secret_key, String type, String symbol, String order_id) throws HttpException, IOException {
		// 构造参数签名
		Map<String, String> params = new HashMap<String, String>();
		params.put("api_key", api_key);
		if(!StringUtil.isEmpty(type)){
			params.put("type", type);
		}
		if(!StringUtil.isEmpty(symbol)){
			params.put("symbol", symbol);
		}
		if(!StringUtil.isEmpty(order_id)){
			params.put("order_id", order_id);
		}

		String sign = MD5Util.buildMysignV1(params, secret_key);
		params.put("sign", sign);
		
		// 发送post请求
		HttpUtilManager httpUtil = HttpUtilManager.getInstance();
		String result = httpUtil.requestHttpPost(url_prex, this.ORDERS_INFO_URL, params);

		return result;
	}

	@Override
	public String order_history(String api_key, String secret_key, String symbol, String status,
			String current_page, String page_length) throws HttpException, IOException {
		// 构造参数签名
		Map<String, String> params = new HashMap<String, String>();
		params.put("api_key", api_key);
		if(!StringUtil.isEmpty(symbol)){
			params.put("symbol", symbol);
		}
		if(!StringUtil.isEmpty(status)){
			params.put("status", status);
		}
		if(!StringUtil.isEmpty(current_page)){
			params.put("current_page", current_page);
		}
		if(!StringUtil.isEmpty(page_length)){
			params.put("page_length", page_length);
		}

		String sign = MD5Util.buildMysignV1(params, secret_key);
		params.put("sign", sign);
		
		// 发送post请求
		HttpUtilManager httpUtil = HttpUtilManager.getInstance();
		String result = httpUtil.requestHttpPost(url_prex, this.ORDER_HISTORY_URL, params);

		return result;
	}

	@Override
	public String withdraw(OkWithdrawRequestVO vo) throws HttpException, IOException {
		Map<String, String> params = new HashMap<String, String>();
		params.put("api_key", vo.getApi_key());
		params.put("symbol", vo.getSymbol());
		params.put("chargefee", vo.getChargefee().toString());
		params.put("trade_pwd", vo.getTrade_pwd());
		params.put("withdraw_address", vo.getWithdraw_address());
		params.put("withdraw_amount", vo.getWithdraw_amount().toString());
		params.put("target", vo.getTarget());

		String sign = MD5Util.buildMysignV1(params, vo.getSecret_key());
		params.put("sign", sign);
		
		HttpUtilManager httpUtil = HttpUtilManager.getInstance();
		String result = httpUtil.requestHttpPost(url_prex, this.WITHDRAW_URL, params);

		return result;
	}

	@Override
	public String cancel_withdraw(OkCancelWithdrawRequestVO vo) throws HttpException, IOException {
		Map<String, String> params = new HashMap<String, String>();
		params.put("api_key", vo.getApi_key());
		params.put("symbol", vo.getSymbol());
		params.put("withdraw_id", vo.getWithdraw_id());

		String sign = MD5Util.buildMysignV1(params, vo.getSecret_key());
		params.put("sign", sign);
		
		HttpUtilManager httpUtil = HttpUtilManager.getInstance();
		String result = httpUtil.requestHttpPost(url_prex, this.CANCEL_WITHDRAW_URL, params);

		return result;
	}

	@Override
	public String withdraw_info(OkWithdrawInfoRequestVO vo) throws HttpException, IOException {
		Map<String, String> params = new HashMap<String, String>();
		params.put("api_key", vo.getApi_key());
		params.put("symbol", vo.getSymbol());
		params.put("withdraw_id", vo.getWithdraw_id());

		String sign = MD5Util.buildMysignV1(params, vo.getSecret_key());
		params.put("sign", sign);
		
		HttpUtilManager httpUtil = HttpUtilManager.getInstance();
		String result = httpUtil.requestHttpPost(url_prex, this.WITHDRAW_INFO_URL, params);

		return result;
	}

	@Override
	public String account_records(OkAccountRecordsRequestVO vo) throws HttpException, IOException {
		Map<String, String> params = new HashMap<String, String>();
		params.put("api_key", vo.getApi_key());
		params.put("symbol", vo.getSymbol());
		params.put("type", vo.getType().toString());
		params.put("current_page", vo.getCurrent_page().toString());
		params.put("page_length", vo.getPage_length().toString());

		String sign = MD5Util.buildMysignV1(params, vo.getSecret_key());
		params.put("sign", sign);
		
		HttpUtilManager httpUtil = HttpUtilManager.getInstance();
		String result = httpUtil.requestHttpPost(url_prex, this.ACCOUNT_RECORDS_URL, params);

		return result;
	}

	@Override
	public String funds_transfer(OkFundsTransferRequestVO vo) throws HttpException, IOException {
		Map<String, String> params = new HashMap<String, String>();
		params.put("api_key", vo.getApi_key());
		params.put("symbol", vo.getSymbol());
		params.put("amount", vo.getAmount().toString());
		params.put("from", vo.getFrom().toString());
		params.put("to", vo.getTo().toString());

		String sign = MD5Util.buildMysignV1(params, vo.getSecret_key());
		params.put("sign", sign);
		
		HttpUtilManager httpUtil = HttpUtilManager.getInstance();
		String result = httpUtil.requestHttpPost(url_prex, this.FUNDS_TRANSFER_URL, params);

		return result;
	}

	@Override
	public String wallet_info(OkWalletInfoRequestVO vo) throws HttpException, IOException {
		Map<String, String> params = new HashMap<String, String>();
		params.put("api_key", vo.getApi_key());

		String sign = MD5Util.buildMysignV1(params, vo.getSecret_key());
		params.put("sign", sign);
		
		HttpUtilManager httpUtil = HttpUtilManager.getInstance();
		String result = httpUtil.requestHttpPost(url_prex, this.WALLET_INFO_URL, params);

		return result;
	}

}
